package com.fengmi.goods.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fengmi.comm.ResultVO;
import com.fengmi.goods.MallGoods;
import com.fengmi.goods.MallGoodsBrand;
import com.fengmi.goods.MallGoodsCat;
import com.fengmi.goods.MallGoodsSku;
import com.fengmi.goods.mapper.MallGoodsBrandMapper;
import com.fengmi.goods.mapper.MallGoodsCatMapper;
import com.fengmi.goods.mapper.MallGoodsMapper;
import com.fengmi.goods.mapper.MallGoodsSkuMapper;
import com.fengmi.goods.service.IMallGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fengmi.goods.vo.MallGoodsAndCatVO;
import com.fengmi.goods.vo.SpuSpecVO;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sujie
 * @since 2022-11-22
 */
@Service
public class MallGoodsServiceImpl extends ServiceImpl<MallGoodsMapper, MallGoods> implements IMallGoodsService {


    @Autowired
    MallGoodsCatMapper mallGoodsCatMapper;
    @Autowired
    MallGoodsBrandMapper mallGoodsBrandMapper;
    @Autowired
    MallGoodsSkuMapper mallGoodsSkuMapper;
    @Autowired
    RocketMQTemplate rocketMQTemplate;
    @Override
    public List<MallGoodsAndCatVO> getAllGoodsAndCat(Integer topn) {

        List<MallGoodsAndCatVO> cats= new ArrayList<MallGoodsAndCatVO>();
        //获取关联商品的三级分类
        List<MallGoodsCat> allGoodsCat3 = mallGoodsCatMapper.getAllGoodsCat3();
        for(MallGoodsCat mallGoodsCat:allGoodsCat3){
            MallGoodsAndCatVO mallGoodsAndCatVO = new MallGoodsAndCatVO();
            //获取分类下的热销商品信息
            List<MallGoods> hotGoodsByCat3 = this.baseMapper.getHotGoodsByCat3(mallGoodsCat.getId(), topn);
            mallGoodsAndCatVO.setCat3(mallGoodsCat);
            mallGoodsAndCatVO.setMallGoods(hotGoodsByCat3);
            //将分类信息及商品信息存到list返回
            cats.add(mallGoodsAndCatVO);
        }
        return cats;
    }

    @Override
    public List<MallGoods> getAllGoods() {

        return this.baseMapper.getAllGoods();
    }

    @Override
    public ResultVO goodsAudit(Long spuId) {
        //根据商品id 查询
        MallGoods mallGoods = this.baseMapper.selectById(spuId);
        if(mallGoods==null){
            return  new ResultVO("1001","商品不存在",null);
        }
        mallGoods.setAuditStatus("1");
        //更新商品的状态改为1
        this.baseMapper.updateById(mallGoods);

        //发送mq消息
        MallGoodsBrand mallGoodsBrand = mallGoodsBrandMapper.selectById(mallGoods.getBrandId());
        mallGoods.setBrand(mallGoodsBrand);
        MallGoodsCat mallGoodsCat1 = mallGoodsCatMapper.selectById(mallGoods.getCategory1Id());
        mallGoods.setCat1(mallGoodsCat1);
        MallGoodsCat mallGoodsCat2 = mallGoodsCatMapper.selectById(mallGoods.getCategory2Id());
        mallGoods.setCat2(mallGoodsCat2);
        MallGoodsCat mallGoodsCat3 = mallGoodsCatMapper.selectById(mallGoods.getCategory3Id());
        mallGoods.setCat3(mallGoodsCat3);
        SendResult sendResult = rocketMQTemplate.syncSend("goods-search:audit", mallGoods);
        SendResult sendResult1 = rocketMQTemplate.syncSend("goods-html:audit", mallGoods.getSpuId());
        if(sendResult.getSendStatus()== SendStatus.SEND_OK && sendResult1.getSendStatus()==SendStatus.SEND_OK ){  //发送成功
            return  new ResultVO("1000","审核通过",null);
        }
         return  new ResultVO("1001","审核失败",null);
    }

    @Override
    public MallGoods getGoodsAndSku(Long spuId) {
        MallGoods mallGoods = this.baseMapper.selectById(spuId);
        if(mallGoods==null){
            return  null;
        }
        QueryWrapper<MallGoodsSku> queryWrapper = new QueryWrapper<>() ;
        queryWrapper.eq("spu_id",mallGoods.getSpuId());
        List<MallGoodsSku> mallGoodsSkus = mallGoodsSkuMapper.selectList(queryWrapper);
        mallGoods.setSkuList(mallGoodsSkus);
      //[{"name":"口味","opts":["原味","咸香"]},{"name":"包装","opts":["情侣装","全家福"]}]

        String spuSpecInfo = mallGoods.getSpuSpecInfo();
        List<SpuSpecVO> spuSpecVOS = JSONUtil.toList(spuSpecInfo, SpuSpecVO.class);
        mallGoods.setSpus(spuSpecVOS);
        return mallGoods;
    }
}
